﻿using System.Windows;
using System.Windows.Media;
using uTrade.Core;

namespace uTrade.Controls
{
    public class ZereVerticalLine : DrawObjBase
    {
        public DataSeries Data;

        protected override int TotalItemCount { get => Data.Count; }

        //绘制以0为基准的垂线，类似MACD线
        protected override void DrawObj(DrawingContext dc)
        {
            // 1.Get Max and min
            double highest = 0;
            double lowest = double.MaxValue;

            var h = Data.Highest(drawItemStartIndex, drawItemCount);
            if (h > highest)
            {
                highest = h;
            }
            var l = Data.Lowest(drawItemStartIndex, drawItemCount);
            if (l < lowest)
            {
                lowest = l;
            }
            if (highest < -lowest)
            {
                highest = -lowest;
            }

            var Vals = Data;
            for (var i = 0; i < drawItemCount; ++i)
            {
                var itemIndex = drawItemStartIndex + i;
                var xoffset = (int)(i * (itemWidth + itemSpace)) + 0.5 + ChartStartX;
                var yoffset = ChartHeight - (int)((Vals[itemIndex] + highest) / (2 * highest) * ChartHeight) + 0.5 + ChartStartY;
                if (itemIndex > 0)
                {
                    if (Vals[itemIndex] * Vals[itemIndex - 1] < 0) //正负交汇处画粗线
                    {
                        if (Vals[itemIndex - 1] < 0)
                        {
                            Pen gpen = new Pen(Brushes.Green, 2.5); //绿色
                            dc.DrawLine(gpen, new Point(xoffset, ChartStartY + ChartHeight / 2 - 4), new Point(xoffset, ChartStartY + ChartHeight / 2 + 4));
                        }
                        if (Vals[itemIndex - 1] > 0)
                        {
                            Pen rpen = new Pen(Brushes.Red, 2.5); //红色
                            dc.DrawLine(rpen, new Point(xoffset, ChartStartY + ChartHeight / 2 - 4), new Point(xoffset, ChartStartY + ChartHeight / 2 + 4));
                        }
                        continue;
                    }
                }
                if (Vals[itemIndex] > 0)
                {
                    Pen gpen = new Pen(Brushes.Green, 1); //绿色
                    dc.DrawLine(gpen, new Point(xoffset, ChartStartY + ChartHeight / 2 + 0.5), new Point(xoffset, yoffset));
                }
                else
                {
                    Pen rpen = new Pen(Brushes.Red, 1); //红色
                    dc.DrawLine(rpen, new Point(xoffset, ChartStartY + ChartHeight / 2 + 0.5), new Point(xoffset, yoffset));
                }
            }
        }
    }
}